﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Threading;
using Caliburn.Micro;
using VoiceMessager.Models;
using VoiceMessager.ViewModels;

namespace VoiceMessager
{
	public class Bootstrapper:BootstrapperBase
	{
		private SimpleContainer _container;

		public Bootstrapper()
		{
			Initialize();
		}

		protected override void Configure()
		{
			_container = new SimpleContainer();
			_container.Instance(_container);
			_container
				.Singleton<IWindowManager, WindowManager>()
				.Singleton<IEventAggregator, EventAggregator>()
				.Singleton<CurrentSelection>()
				.Singleton<MainViewModel>()
				.PerRequest<ConnectViewModel>()
				.PerRequest<VerifyViewModel>()
				.PerRequest<SendFrameViewModel>()
				;
		}

		protected override void OnStartup(object sender, StartupEventArgs e)
		{
			DisplayRootViewFor<MainViewModel>();
		}

		protected override object GetInstance(Type serviceType, string key)
		{
			return _container.GetInstance(serviceType, key);
		}

		protected override IEnumerable<object> GetAllInstances(Type serviceType)
		{
			return _container.GetAllInstances(serviceType);
		}

		protected override void BuildUp(object instance)
		{
			_container.BuildUp(instance);
		}

		protected override void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
		{
			e.Handled = true;
			MessageBox.Show(e.Exception.Message, "An error has occurred", MessageBoxButton.OK);
		}
	}
}
